package com.bsdsma.visonManagementSystemapp.restcontroller;

import com.bsdsma.visonManagementSystemapp.constants.ErrorCodes;
import com.bsdsma.visonManagementSystemapp.exceptionhandler.GlobalRuntimeException;
import com.bsdsma.visonManagementSystemapp.dto.BaogaoAddDto;
import com.bsdsma.visonManagementSystemapp.dto.BaogaoSearchDto;
import com.bsdsma.visonManagementSystemapp.dto.DataGroupByDateDto;
import com.bsdsma.visonManagementSystemapp.dto.BaogaoDto;
import com.bsdsma.visonManagementSystemapp.service.BaogaoService;
import com.bsdsma.visonManagementSystemapp.utils.ResultUtils;
import com.bsdsma.visonManagementSystemapp.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 视力报告模块对外暴露api入口, 即可通过http请求访问到下面各个方法
 */
@RestController
@RequestMapping(value = "/api/baogao")
public class BaogaoRestController {

    @Autowired
    private BaogaoService baogaoService;

    /**
     * 分页查询视力报告, 接收"HTTP GET"请求
     * @param search    入参, 查询条件对象
     */
    @GetMapping
    public Map<String, Object> search(BaogaoSearchDto search){
        return ResultUtils.ok(baogaoService.search(search));
    }

    /**
    * 查询所有视力报告, 接收"HTTP GET"请求
    */
    @GetMapping(value = "/list")
    public Map<String, Object> listAll(){
        return ResultUtils.ok(baogaoService.listAll());
    }

    /**
    * 通过主键ID查询视力报告, 接收"HTTP GET"请求
    */
    @GetMapping(value = "/{id}")
    public Map<String, Object> getById(@PathVariable Long id){
        return ResultUtils.ok(baogaoService.getById(id));
    }

    /**
     * 添加视力报告数据到数据库, 接收"HTTP POST"请求
     * @param entity    入参, 添加的字段对象
     */
    @PostMapping
    public Map<String, Object> add(@RequestBody BaogaoAddDto entity){
        return ResultUtils.ok(baogaoService.add(entity));
    }

    /**
     * 更新视力报告数据到数据库, 接收"HTTP POST"请求
     * @param id        主键ID, 更新需要一个唯一标识, 也就是主键ID
     * @param entity    入参, 更新的字段对象
     */
    @PutMapping("/{id}")
    public Map<String, Object> update(@PathVariable Long id, @RequestBody BaogaoAddDto entity){
        return ResultUtils.ok(baogaoService.update(id, entity));
    }

    /**
     * 删除视力报告数据, 接收"HTTP DELETE"请求
     * @param id    主键ID, 删除与更新一样, 也需要一个唯一标识
     */
    @DeleteMapping(value = "/{id}")
    public Map<String, Object> delete(@PathVariable Long id){
        baogaoService.delete(id);
        return ResultUtils.ok();
    }

    /**
    * 查询视力报告数量, 接收"HTTP GET"请求
    */
    @GetMapping(value = "/count")
    public Map<String, Object> count(){
        Long count = baogaoService.count();
        return ResultUtils.ok(null == count ? 0L : count);
    }

    /**
    * 日新增统计
    */
    @GetMapping(value = "/day-newly-added")
    public Map<String, Object> dayNewlyAdded(){
        // 今天
        Date endDate = DateUtils.getNow();
        // 30天以前
        Date beginDate = DateUtils.dayAfter(endDate, -30);
        List<DataGroupByDateDto> dataList = baogaoService.countByDay(beginDate, endDate);
        return ResultUtils.ok(dataList);
    }

    /**
     * Get all reports for a student
     */
    @GetMapping("/student/{xuehao}")
    public Map<String, Object> getByXuehao(@PathVariable Long xuehao) {
        return ResultUtils.ok(baogaoService.getByXuehao(xuehao));
    }

    /**
     * Get all reports for a school
     */
    @GetMapping("/school/{school}")
    public Map<String, Object> getBySchool(
            @PathVariable String school,
            @RequestParam(required = false) Integer year) {
        return ResultUtils.ok(baogaoService.getBySchool(school, year));
    }

    /**
     * Get all reports (admin only)
     */
    @GetMapping("/all")
    public Map<String, Object> getAll(@RequestParam(required = false) Integer year) {
        return ResultUtils.ok(baogaoService.getAll(year));
    }

    /**
     * Get current year's report for a student
     */
    @GetMapping("/current/{xuehao}")
    public Map<String, Object> getCurrentYearReport(@PathVariable Long xuehao) {
        return ResultUtils.ok(baogaoService.getCurrentYearReport(xuehao));
    }
}
